<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/jquery.easyui.min.js"></script>
    <script src="../js/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" href="../themes/icon.css">
    <link rel="stylesheet" href="../themes/default/easyui.css">
    <link rel="stylesheet" href="../css/wu.css">
    <style type="text/css">
        .subck { display:none }/*合计单元格样式*/
        .datagrid-row-selected{
		    background-color:none;
		}
    </style>
    <script>
        var  selectIndex = -1;
        function getExcel(){
            $.messager.confirm('确认','您确认想要导出excel文件吗？（下载路径为浏览器默认下载路径）',function(r) {
                if (r) {
                    var options = $("#productList").datagrid("getPager").data("pagination").options;
					var curr = options.pageNumber;  
					var size = options.pageSize;
                     var findcode=$("#findcode").val();
                    var findstartDate=$("#findstartDate").val();
                    var findendDate=$("#findendDate").val();
                    var findsupplierName=$("#findsupplierName").val();
                    location.href="/kxmanage/instroageBill/getExcel.json?findcode="+findcode+"&findstartDate="+findstartDate
						+"&findendDate="+findendDate+"&findsupplierName="+findsupplierName+"&page="+curr+"&rows="+size; 
                }
            });
        }
        function hideTable(){
            $("#searchdiv").toggle();
        }
        function openaddinstroagebill(){
            self.parent.$("#jxc_tabs").tabs("close","增加入库单");
            self.parent.addTabs('增加入库单','goods/addInstroageBill.jsp');
        }
        
        function reloaddata(){
        	$("#form1").form('reset');
			$("#productList").datagrid('load',{});
        }
        
        $(function(){
            /* self.parent.$("#jxc_tabs").tabs({
                onSelect:function(title,index){
                    if(title=="入库单管理"){
                        $("#productList").datagrid("reload");
                    }
                }
            }) */
           	/* var data=$("#productList").datagrid('getData');
            alert('总数据量:'+data.total) */	
            //数据表格
            $("#productList").datagrid({
                url:'/kxmanage/instroageBill/getInstroageBillList.json',
                rownumbers:false,
                pageNumber:1,
                pageSize:5,
                pageList:[5,10,15],
                checkOnSelect:false,
                columns:[[
                    {field:'ck',checkbox:true,
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1;';
                            }
                        }
                    
                    },
                    {field:'code',title:'入库单号',width:150,
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'instroageDate',title:'入库时间',width:100,
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }},
                    {field:'customName',title:'供应商名称',width:200,
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }, formatter:function(value,row,index){
                            if(row.isFooter){
                                return row.customName;
                            }
                            return row.customName;
                    }},
                    {field:'bamount',title:'数量',width:40,
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }, formatter:function(value,row,index){
                            if(row.isFooter){
                                return row.bamount;
                            }
                            return row.bamount;
                    }},
                    {field:'money',title:'总货值(￥)',width:100,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return row.money+"￥";
                            }
                            return row.money+"￥";
                    },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'tuihuo',title:'退货情况',width:60,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return "";
                            }
                            return "0.00";
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'fukuan',title:'付款情况',width:60,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return "";
                            }
                            return "0.00";
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'shoupiao',title:'收票情况',width:60,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return "";
                            }
                            return "0.00";
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'checkStatus',title:'审核情况',width:60,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return "";
                            }
                            if(row.checkStatus==1){
                                return "未审核";
                            }else{
                                return "已审核";
                            }
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                            if(row.checkStatus==1){
                                return "color:red";
                            }
                        }
                    },
                    {field:'userName',title:'操作员',width:100,
                        formatter:function(value,row,index){
                            if(row.isFooter){
                                return "";
                            }
                            return "${user.userName}";
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }
                    },
                    {field:'opt',title:'操作',width:100,formatter:function (value,row,index) {
                            if(row.isFooter){
                                return "";
                            }
                            var context="<a href=\"javascript:modifyInstroageBill('"+row.code+"')\">修改</a>&nbsp;&nbsp;" +
                                "<a href=\"javascript:delrow('"+row.code+"','"+row.checkStatus+"')\">删除</a>"
                            return context;
                        },
                        styler: function (value, row, index) {
                            if(row.isFooter){
                                return 'background-color:#F1F1F1';
                            }
                        }}
                ]],
                toolbar:'#wu_toolbar',
                showFooter:true,
                pagination:true ,//向后台传递参数page 和rows
                onClickRow: function(index, data) {
                    //$(this).datagrid('usw', index);
                },
                onDblClickRow: function (index, row) {
                    $("#billDetails").show();
                    $("#billCode").html("单据标号为："+row.code+" 的明细如下所列！")
                    loadDetailList(row.code);
                },
                onLoadSuccess:function(data){
                    /* $('#productList').datagrid('appendRow', {
	                customName: '<span class="subtotal">合计</span>',
	                bamount: '<span class="subtotal">' + compute("bamount") + '</span>',
	                money: '<span class="subtotal">' + compute("money") + '</span>',
	                //Rate: '<span class="subtotal">' + ((compute("TotalOrderScore") / compute("TotalTrailCount")) * 100).toFixed(2) + '</span>'
                    }); */
                },
                 rowStyler: function (index, row) {
	                var rows = $("#productList").datagrid("selectRow"); 
	                if (rows) {
	                    return 'background-color:#ffffff;';
	                } else {
	                    return 'background-color:#ffffff;';
	                }
	            }
            })
            
        $("#search_btn").click(function(){
            $("#productList").datagrid('load',serializeJson())
        }) 
        function loadDetailList(data){
            $("#billDetailList").datagrid({
                        fitColumns:true,
		                url:'/kxmanage/instroageBill/getBillDetailList.json',
		                rownumbers:true,
		                checkOnSelect:false,
		                queryParams: {
							billCode:data
	                    },
	                    showFooter:true,
		                columns:[[
		                    {field:'orderCode',title:'订单编号',width:150},
		                    {field:'partNo',title:'件号',width:100,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return ;  
                                    }
		                            return row.part.partNo;
		                        }
		                    },
		                    {field:'partName',title:'配件名称',width:200,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return row.partName;  
                                    }
		                            return row.part.partName;
		                        }
		                    },
		                    {field:'partBrand',title:'配件品牌',width:100,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return;  
                                    }
		                            return row.part.partBrand;
		                        }
		                    },
		                    {field:'partTypeName',title:'配件型号',width:100,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return;  
                                    }
		                            return row.part.partTypeName;
		                        }
		                    },
		                    {field:'amount',title:'数量',width:60,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return row.amount;  
                                    }
		                            return row.amount;
		                        }
		                    },
		                    {field:'price',title:'单价(￥)',width:60,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return "";  
                                    }
		                            return row.price+"￥";
		                        }},
		                    {field:'money',title:'金额(￥)',width:100,
		                        formatter:function(value,row,index){
		                            if(row.isFooter){   //判断出是否footer  
                                        return row.money+"￥";  
                                    }
		                            return row.price*row.amount+"￥";
		                        }
		                    },
		                    {field:'memo',title:'备注',width:200}
		                ]]
		           });
              }
        });
        function detail(id) {
            $.messager.alert("信息","查看"+id+"产品详情");
        }
        function modifyInstroageBill(code) {
            self.parent.$("#jxc_tabs").tabs("close","修改入库单");
            parent.addTabs("修改入库单","../instroageBill/modifyInstroageBill.html?code="+code);
        }
        function addProduct() {
            parent.addTabs("添加产品","goods/add_goods.html");
        }
        function delrow(code,status){
            if(status==2){
                alert("已审核，不可删除！");
                return false;
            }
             $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    var ids=[];
                    ids.push(code);
                    $.post("/kxmanage/instroageBill/delrows.json",{"codes":ids},resultDel);
                }
            });
        }
        function resultDel(data){
            if(data.result=='success'){
            	$("#billDetails").hide();
                $.messager.alert('消息'," 删除成功！");
                $("#productList").datagrid('load',{});
            }else{
                $.messager.alert('消息'," 删除失败！");
            }
        }
        //删除
        function deleteRightGridRow(rowId){    	
	        var datas = $("#productList").datagrid('getData');    	
	        for(var i=0;i<datas.rows.length;i++){    	    
		        if(datas.rows[i].id==rowId){//数据中的id和渲染时的id相等    	    	
		            //通过传入的id值查询到对应的记录，在获取实际的Index,这样去删除,（直接传入渲染好的索引值会出现错误）		  		
			        var rowIndex = $("#productList").datagrid('getRowIndex',datas.rows[i]);		  		 
			        $("#productList").datagrid('deleteRow', rowIndex);		  		 
			        return;    		
		        }   		 
	        } 
         }
        //刷新入库单
         function addtabs(){
             self.parent.addTabs("入库单管理","goods/instroageBillList.jsp");
            }
        function del() {
            var rows=$("#productList").datagrid('getSelections');
            if(rows.length==0){
                $.messager.alert('消息',"请选择记录");
                return
            }
            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    var ids=[]
                    for( var  i=0;i<rows.length;i++){
                        if(rows[i].checkStatus==2){
                            alert("已审核，不可删除！")
                            return false;
                        }
                        ids.push(rows[i].code);
                    }
                    $.post("/kxmanage/instroageBill/delrows.json",{"codes":ids},resultDel);
                }
            });
        }
       //表单序列化
        function serializeJson(){  
            var serializeObj={};  
            var array=$("#form1").serializeArray();  
            var str=$("#form1").serialize();  
            $(array).each(function(){  
                if(serializeObj[this.name]){  
                    if($.isArray(serializeObj[this.name])){  
                        serializeObj[this.name].push(this.value);  
                    }else{  
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }  
                }else{  
                    serializeObj[this.name]=this.value;   
                }  
            });  
                return serializeObj;  
            }; 
        //指定列求和
        function compute(colName) {
			var rows = $('#productList').datagrid('getRows');
			var total = 0;
			for (var i = 0; i < rows.length; i++) {
				total += parseFloat(rows[i][colName]);
			}
			return total;
		}
		function onLoadSuccess() {
			//添加“合计”列
			$('#table').datagrid('appendRow', {
				Saler: '<span class="subtotal">合计</span>',
				TotalOrderCount: '<span class="subtotal">' + compute("TotalOrderCount") + '</span>',
				TotalOrderMoney: '<span class="subtotal">' + compute("TotalOrderMoney") + '</span>',
				TotalOrderScore: '<span class="subtotal">' + compute("TotalOrderScore") + '</span>',
				TotalTrailCount: '<span class="subtotal">' + compute("TotalTrailCount") + '</span>',
				Rate: '<span class="subtotal">' + ((compute("TotalOrderScore") / compute("TotalTrailCount")) * 100).toFixed(2) + '</span>'
			});
		}
    </script>
</head>
<body>
<div id="wu_toolbar">
    <div class="wu_toolbar_button" style="background-color:#CEDFEF;">
        <a  href="#" onclick="hideTable()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" plain="true"> 查询</a>|
        <a  href="#" onclick="openaddinstroagebill()" class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true">增加</a>|
        <a  href="#" onclick="del()" class="easyui-linkbutton" data-options="iconCls:'icon-delete-self'" plain="true"> 批量删除</a>|
        <a  href="#" onclick="addtabs()" class="easyui-linkbutton" data-options="iconCls:'icon-page-refresh'" plain="true"> 刷新</a>|
        <a  href="#"  class="easyui-linkbutton" data-options="iconCls:'icon-communication'" onclick="getExcel()" plain="true"> 导出excal</a>
    </div>
    <div id="searchdiv" style="display: block;width: 100%;">
        <form name="form1" id="form1">
            <table width="100%" border="0" cellspacing="0" style="background-color:#C9DFFF;border-top:1px solid white;" >
                <tbody><tr>
                    <td width="70"><b>检索条件:<input type="hidden" id="" value=""/></b></td>
                    <td width="70" align="right">入库编号:</td>
                    <td width="150" align="left"><input name="findcode" class="easyui-textbox" type="text" id="findcode" value="" maxlength="100"></td>
                    <td width="70" align="right">开始日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findstartDate" type="text" id="findstartDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findstartDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">结束日期:</td>
                    <td width="150" align="left"><div class="l-text-wrapper"><div class="l-text l-text-date"><input name="findendDate" type="text" id="findendDate" value="" maxlength="30" class="easyui-datebox" ligeruiid="findendDate"><div class="l-text-l"></div><div class="l-text-r"></div><div class="l-trigger"><div class="l-trigger-icon"></div></div></div></div></td>
                    <td width="70" align="right">供应商名:</td>
                    <td width="150" align="left"><input name="findsupplierName" class="easyui-textbox" type="text" id="findsupplierName" value="" maxlength="100"></td>
                    <td width="72">
                        <a id="search_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
                    </td>
                    <td width="72">
                        <a id="reset_btn" href="#" class="easyui-linkbutton" onclick="reloaddata()" data-options="iconCls:'icon-refresh'">重置</a>
                    </td>
                    <td width="0"></td>
                </tr>
                </tbody>
            </table>
            <input type="hidden" id="menuCode" value="100001001">
        </form>
    </div>
</div>
<div id="productList" class="easyui-datagrid">

</div>
<div id="billDetails">
    <div id="billCode" style="font-weight:bolder;font-size:14px;"></div>
    <div id="billDetailList" class="easyui-datagrid">
      
    </div>
</div>
            

</body>
</html>